/*
 */
package com.ericsson.springauth.util;

import com.ericsson.springauth.model.User;
import com.ericsson.springauth.model.User;
import java.io.IOException;
import javax.servlet.*;
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 *
 * @author eluibon
 */
public class LogLoggedUserFilter implements Filter {

    protected Logger log = Logger.getLogger(this.getClass().getName());
    
    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig fc) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) throws IOException, ServletException {

        SecurityContext sc = SecurityContextHolder.getContext();
        if (sc != null) {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            //log.debug("Filtering user " + user.getLastName());
            
            MDC.put("lu", user.getId());
        }
        
        fc.doFilter(sr, sr1);
    }
}
